package com.sanjiahantang.youjiarole.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.R;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.http.BaseDelegate;
import com.sanjiahantang.youjiarole.http.OkHttpClientManager;
import com.sanjiahantang.youjiarole.module.common.beans.StringBean;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.okhttp.Request;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by sks on 2015/10/22.
 * 自定义dialog
 */
public class SendMessageDialog extends Dialog {

    private TextView txtDialogTitle;
    private TextView txtDialogContent;
    private TextView txtCode, cancel, sure;
    private EditText ed_code;


    private String title, content, leftText, rightText;
    public OnSureClickListener onClickListener;
    private Activity context;
    private Timer timer;
    private int startime = 60;
    String code;

    public SendMessageDialog(Context context) {
        super(context);
    }

    public SendMessageDialog(Activity context, String themeResId, String title,
                             OnSureClickListener onClickListener) {
        super(context);
        this.context = context;
        this.title = title;
        this.onClickListener = onClickListener;
    }

    protected SendMessageDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_sendmessage);
        txtCode = (TextView) findViewById(R.id.txtCode);
        txtCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        startime--;
                        Bundle bundle = new Bundle();
                        bundle.putInt("code", startime);
                        Message mag = new Message();
                        mag.setData(bundle);
                        han.sendMessage(mag);
                    }
                }, startime, 1000);
                sendCode();
            }
        });
        ed_code = (EditText) findViewById(R.id.txtNameNO);

        txtDialogTitle = (TextView) findViewById(R.id.txtDialogTitle);
        txtDialogContent = (TextView) findViewById(R.id.txtDialogContent);
        cancel = (TextView) findViewById(R.id.txtDialogCancel);
        sure = (TextView) findViewById(R.id.txtDialogSure);
        txtDialogTitle.setText(title);
        txtDialogContent.setText(content);
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                onClickListener.getText(ed_code, ed_code.getText().toString());
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        Window dialogWindow = getWindow();
        /*
         * 将对话框的大小按屏幕大小的百分比设置
         */

        WindowManager m = context.getWindowManager();
        Display d = m.getDefaultDisplay();// 获取屏幕宽、高
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
//        p.height = (int) (d.getHeight() * 0.6); // 高度设置为屏幕的0.6
        p.width = (int) (d.getWidth() * 0.85); // 宽度设置为屏幕的0.85
        dialogWindow.setAttributes(p);

    }

    //发送验证码
    private void sendCode() {
        Map<String, Object> map = new HashMap<>();
        map.put(StaticValue.KEY_TOKEN, ConfigValue.DATA_KEY);
        map.put(StaticValue.KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, ""));
        OkHttpClientManager.postJson(context, ConfigValue.API_URL + "/money/phone/role/findMoneyInfo", map, new BaseDelegate.ResultCallback<StringBean>() {
            @Override
            public void onError(Request request, Object tag, Exception e) {

            }

            @Override
            public void onResponse(StringBean response, Object tag) {
                Utils.showToast(context, response.getMessage());
            }
        });
    }

    private Handler han = new Handler() {
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            int s = bundle.getInt("code");
            if (s < 0) {
                timer.cancel();
                txtCode.setClickable(true);
                txtCode.setText("发送消息");
                txtCode.setTextColor(getContext().getResources().getColor(R.color.colorPrimary));
                txtCode.setBackgroundResource(R.drawable.x_shape_green_border);
                startime = 60;
            } else {
                txtCode.setText("重新获取" + "(" + s + ")");
                txtCode.setTextColor(getContext().getResources().getColor(R.color.gray));
                txtCode.setClickable(false);
                txtCode.setBackgroundResource(R.drawable.x_shape_gray_border_r20);
            }
        }

    };

    public interface OnSureClickListener {
        void getText(View view, String string); // 声明获取EditText中数据的接口
    }
}
